﻿using Abp.Domain.Services;
using Abp.ObjectMapping;
using Abp.Runtime.Caching;
using Abp.UI;
using AVIS.OutBound.Wizard.Cache;
using AVIS.OutBound.WizardAgent;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AVIS.OutBound.Wizard
{
    /// <summary>
    /// Wizard数据处理的服务类
    /// </summary>
    public class WizardManager : DomainService, IWizardManager
    {
        private readonly IWizardRepository _wizardRepository;
        private readonly IObjectMapper _objectMapper;
        private readonly ICacheManager _cacheManager;

        public WizardManager(IWizardRepository wizardRepository,
            IObjectMapper objectMapper,
            ICacheManager cacheManager)
        {
            _wizardRepository = wizardRepository;
            _objectMapper = objectMapper;
            _cacheManager = cacheManager;
        }

        /// <summary>
        /// 处理Wizard结果的错误信息
        /// </summary>
        /// <param name="response">Wizard请求结果</param>
        public void HandleErrorMessage(IResponse response)
        {
            // 处理提醒信息
            if (response.WarningCode != 0 || !string.IsNullOrWhiteSpace(response.WarningMessage))
            {
                // TODO 如何传递给用户 转换为中文的错误信息
                var warningMsg = GetErrorMessage(response.WarningCode.ToString());
                response.WarningMessage = warningMsg != null ? warningMsg.CNErrorElement : response.WarningMessage;
            }

            // 处理错误信息
            if (response.ErrorCode != 0)
            {
                // 转换为中文的错误信息
                var errorMsg = GetErrorMessage(response.ErrorCode.ToString());
                response.ErrorMessage = errorMsg != null ? errorMsg.CNErrorElement : response.ErrorMessage;
            }

            // 如果有错误信息，则抛出异常
            if (!string.IsNullOrWhiteSpace(response.ErrorMessage))
            {
                throw new UserFriendlyException(response.ErrorMessage);
            }
        }

        /// <summary>
        /// 获取所有价格包含（PriceInclude）的中文解释
        /// </summary>
        /// <returns></returns>
        public IDictionary<string, string> GetPriceIncludeTranslations()
        {
            var cache = _cacheManager.GetCache("AvisAppPriceIncludeCache");
            var dicPriceInclude = cache.Get("ALLPriceIncludes",
                f => _wizardRepository.GetAllPriceIncludes()) as Dictionary<string, string>;

            return dicPriceInclude ?? new Dictionary<string, string>();
        }

        /// <summary>
        /// 获取所有价格包含（PriceInclude）的简写中文解释
        /// 注意：+号表示一条内容里面同时包含关键字，有 +号 需要排在前面
        /// </summary>
        /// <returns></returns>
        public IDictionary<string, string> GetSimplePriceIncludeTranslations()
        {
            var cache = _cacheManager.GetCache("AvisAppPriceIncludeCache");
            var dicPriceInclude = cache.Get("ALLSimplePriceIncludes",
                f => _wizardRepository.GetAllSimplePriceIncludes()) as Dictionary<string, string>;

            return dicPriceInclude ?? new Dictionary<string, string>();
        }

        /// <summary>
        /// 获取指定错误代码的错误信息
        /// </summary>
        /// <param name="ErrorCode"></param>
        /// <returns></returns>
        public ErrorMessage GetErrorMessage(string ErrorCode)
        {
            var cache = _cacheManager.GetErrorMessageCache().Get(ErrorCode, f => GetErrorCacheItem(ErrorCode));
            var Error = _objectMapper.Map<ErrorMessage>(cache);
            return Error;
        }


        private ErrorMessageCacheItem GetErrorCacheItem(string ErrorCode)
        {
            var data = _wizardRepository.GetErrorMessage(ErrorCode);
            var Error = _objectMapper.Map<ErrorMessageCacheItem>(data);
            return Error;
        }
    }
}
